package com.lcj.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lcj.eduService.client.VodClient;
import com.lcj.eduService.entity.EduVideo;
import com.lcj.eduService.mapper.EduVideoMapper;
import com.lcj.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-14
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
    private VodClient vodClient;

    @Override
    public boolean deleteByCourseId(String courseId) {

        //根据课程id查询视频id
        QueryWrapper<EduVideo> wrapperIds =new QueryWrapper<>();
        wrapperIds.eq("course_id",courseId);
        wrapperIds.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wrapperIds);
        List<String> videoIds = new ArrayList<>();
        for(EduVideo eduVideo : eduVideos){
            if(StringUtils.isEmpty(eduVideo.getVideoSourceId()))
            videoIds.add(eduVideo.getVideoSourceId());
        }
        if(videoIds.size() >0){
            vodClient.deleteBatch(videoIds);
        }
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        Integer count = baseMapper.delete(wrapper);
        return null != count && count > 0;
    }
}
